﻿using log4net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;

namespace ConfigurationLearning.Controllers
{
    public class HomeController : Controller
    {
        readonly ILog _log;
        public HomeController(ILog log)
        {
            this._log = log;
        }

        private const string InternalConfigSettingsFactoryTypeString = "System.Configuration.Internal.InternalConfigSettingsFactory, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
        private const string HttpConfigurationSystemTypeString = "System.Web.Configuration.HttpConfigurationSystem, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";

        public ActionResult Index()
        {
            //TestUpdateWebConfig();

            LogTest();

            return View();
        }


        void LogTest()
        {
            this._log.Error("error");
        }


        void Test()
        {
            List<Type> TypeItemList = new List<Type>();
            Assembly[] AssbyCustmList = System.AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assItem in AssbyCustmList)
            {
                TypeItemList.AddRange(assItem.GetTypes().ToList());
            }

            //var internalConfigSettingFactoryType = TypeItemList.Where(t => t.FullName.Contains("InternalConfigSettingsFactory"));
            //var httpConfigurationSystemType = TypeItemList.Where(t => t.FullName.Contains("HttpConfigurationSystem"));
            //var ts = Assembly.GetExecutingAssembly().GetTypes();
            //HttpConfigurationSystem configSystem = new HttpConfigurationSystem();
            Type typeFactory = Type.GetType(InternalConfigSettingsFactoryTypeString, true);
            Type httpConfigurationSystem = Type.GetType(HttpConfigurationSystemTypeString, true);
            System.Configuration.Internal.IInternalConfigSystem configSystem = (System.Configuration.Internal.IInternalConfigSystem)Activator.CreateInstance(httpConfigurationSystem, true);
            System.Configuration.Internal.IInternalConfigSettingsFactory s_configSettingsFactory = (System.Configuration.Internal.IInternalConfigSettingsFactory)Activator.CreateInstance(typeFactory, true);
            s_configSettingsFactory.SetConfigurationSystem(configSystem, true);

            FieldInfo s_configSystem = typeof(ConfigurationManager).GetField("s_configSystem", BindingFlags.Static | BindingFlags.NonPublic);
            object clientConfigurationSystem = s_configSystem.GetValue(null);
        }

        private void TestUpdateWebConfig()
        {
            //读取程序集的配置文件
            string assemblyConfigFile = Assembly.GetEntryAssembly()?.Location;
            string appDomainConfigFile = AppDomain.CurrentDomain?.SetupInformation?.ConfigurationFile;

            Configuration config = ConfigurationManager.OpenExeConfiguration(appDomainConfigFile);


            //获取appSettings节点
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");


            //删除name，然后添加新值
            appSettings.Settings.Remove("update");
            appSettings.Settings.Add("update", "updated");

            //保存配置文件
            config.Save();

        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}